package com.otojob.service.impl;

import com.otojob.dto.PageDto;
import com.otojob.entity.Job;
import com.otojob.entity.UserJob;
import com.otojob.entity.vo.JobDetails;
import com.otojob.entity.vo.JobFrontVo;
import com.otojob.entity.vo.ResumeListVO;
import com.otojob.mapper.JobMapper;
import com.otojob.mapper.UserJobMapper;
import com.otojob.service.UserJobService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otojob.utils.JwtUtils;
import com.otojob.utils.Results;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TingFeng
 * @since 2022-03-18
 */
@Service
public class UserJobServiceImpl extends ServiceImpl<UserJobMapper, UserJob> implements UserJobService {
    @Resource
    private UserJobMapper userJobMapper;
    @Resource
    private JobMapper jobMapper;
    @Resource
    private HttpServletRequest request;

    /**
     * 查询已投递工作
     * @return
     */
    @Override
    public Results findJobHistoryByUserId(PageDto pageDto) {
        int id = JwtUtils.getMemberIdByJwtToken(request);

        int numPage=pageDto.getPageNum();
        int numSize=pageDto.getPageSize();

        List<JobFrontVo> listJob = jobMapper.findMyHistoryJob((numPage-1)*numSize,numSize,id);
        for (JobFrontVo jobFrontVo : listJob) {
            String[] split = jobFrontVo.getPlace().split("-");
            jobFrontVo.setPlace(split[0]);
        }
//        int totals = jobMapper.findAllJob();
        int totals = listJob.size();
        int pages = (int) Math.ceil(totals/numSize);

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("numPage",numPage);
        map.put("numSize",numSize);
        map.put("totals",totals);
        map.put("pages",pages);
        map.put("listJob",listJob);
        return Results.ok().data(map);

    }

    /**
     * 查询收到简历列表
     * @return
     */
    @Override
    public Results findReceivedResumeListFront() {
        int userId = JwtUtils.getMemberIdByJwtToken(request);



        List<JobFrontVo> listJob = jobMapper.findJobByUserId(userId);
        for (JobFrontVo jobFrontVo : listJob) {
            String[] split = jobFrontVo.getPlace().split("-");
            jobFrontVo.setPlace(split[0]);
        }

//        HashMap<String, Object> mapList = new HashMap<String, Object>();

        ArrayList<Map<String, Object>> mapList = new ArrayList<>();
//        int totals = jobMapper.findAllJob();
        int totals = listJob.size();
        for (int i = 0; i < totals; i++) {
            HashMap<String, Object> map2 = new HashMap<String, Object>();
            int jobId=listJob.get(i).getId();
            //收到简历份数
            int uJobCount=userJobMapper.findUserIdByJobId(jobId).size();
            String jobName =listJob.get(i).getName();
            map2.put("jobId",jobId);
            map2.put("uJobCount",uJobCount);
            map2.put("jobName",jobName);

            mapList.add(map2);
        }

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("totals",totals);
        map.put("listJob",mapList);
        return Results.ok().data(map);
    }
    /**
     * 查询收到简历
     * @param pageDto
     * @param jobId
     * @return
     */
    @Override
    public Results findReceivedResumeList(PageDto pageDto, Integer jobId) {
        int numPage=pageDto.getPageNum();
        int numSize=pageDto.getPageSize();
        JobDetails jobDetailsByJobId = jobMapper.findJobDetailsByJobId(jobId);
        int uJobCount=userJobMapper.findUserIdByJobId(jobId).size();
        List<ResumeListVO> resumeList = userJobMapper
                .findReceivedResumeList(jobId);
        int i=0;
        ArrayList<Object> objects = new ArrayList<>();
        for (ResumeListVO resumeListVO : resumeList) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",i);
            map.put("nickname",resumeListVO.getNickname());
            map.put("resume",resumeListVO.getResume());
            objects.add(map);
            i++;
        }
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("list",objects);
        stringObjectHashMap.put("name",jobDetailsByJobId.getName());
        stringObjectHashMap.put("count",uJobCount);
        return Results .ok().data(stringObjectHashMap);
    }
}
